/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.stat.content.log;

import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.LogErrorInfo;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.stat.StatMessages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class ViewErrorPanel extends Composite {

	private StatMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context;

	private SimplePanel panel;

	private DialogBox box;
	
	private boolean loaded;

	

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			LogErrorInfo err = (LogErrorInfo) res.getResponse();
			panel.clear();
			if (err != null) {
				Grid table = new Grid(2, 2);
				table.setWidth("100%");
				table.setCellPadding(1);
				table.setCellSpacing(1);
				table.setBorderWidth(1);

				ColumnFormatter cf = table.getColumnFormatter();

				cf.setWidth(0, "25%");
				cf.setWidth(1, "75%");

				CellFormatter ff = table.getCellFormatter();
				ff.setStyleName(0, 0, "tablecol");
				ff.setStyleName(1, 0, "tablecol");

				table.setHTML(0, 0, cons.errorCode());
				table.setHTML(1, 0, cons.errorTrace());

				table.setHTML(0, 1, err.getErrorCode());
				
				ScrollPanel vp = new ScrollPanel();
				vp.setSize("500px", "350px");
				vp.add(new HTML(err.getTrace()));
			    
				table.setWidget(1, 1, vp);
				panel.add(table);
			} else {
				panel.add(new HTML(cons.noError()));
			}
			//panel.add(new HTML("&nbsp;"));

			

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public ViewErrorPanel(StatMessages cons, BifangGWTServiceAsync service,
			GwtContext ctx, DialogBox box1) {
		this.service = service;
		this.cons = cons;
		this.box = box1;
		this.context = ctx;
		VerticalPanel vp = new VerticalPanel();
		vp.setWidth("100%");
		vp.setSpacing(5);
		
		panel = new SimplePanel();
		panel.setWidth("100%");
		vp.add(panel);
		// gIds = new HashMap<String, String>();
		//		
		// for(Usergroup ug : gs){
		// gIds.put(ug.getId(), "");
		// }

		Button re = new Button(cons.close());
		re.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				box.hide();

			}
		});

		vp.add(re);
		
		initWidget(vp);

	}

	public void onLoad() {
		if(loaded)
			return;
		RequestObject req = new RequestObject();
		req.setAction("han.bifang.gwt.server.action.stat.LogDetailAction");
		req.setRequest("error");
		service.call(req, cb);
		loaded = true;
	}

}
